<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>父访问子</title>
  </head>
  <body>
    <div id="app">
      <cpn> </cpn>
      <cpn ref="aaa"></cpn>
      <cpn></cpn>
      <button @click="btnClick">按钮</button>
    </div>
    <template id="cpn">
      <div>
        我是组件
      </div>
    </template>
    <script src="../vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          message: "hello",
        },
        methods: {
          btnClick() {
            //1.$childern
            console.log(this.$children);
            for (let c of this.$children) {
              console.log(c.name);
              c.showMessage();
            }
            // console.log(this.$children[3].name);必须在某个组件加 ref="***"
            //2.$refs 对象类型，默认为空的对象;
            console.log(this.$refs.aaa.name);
          },
        },
        components: {
          cpn: {
            template: "#cpn",
            data() {
              return {
                name: "cpn1",
              };
            },
            methods: {
              showMessage() {
                console.log("showMessage");
              },
            },
          },
        },
      });
    </script>
  </body>
</html>
